﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace FBGraph
{
    /// <summary>Represents an access token granted to an application.</summary>
    [DataContract(Name = "accessToken")]
    public struct AccessToken
    {
        /// <summary>Instantiates an instance of <see cref="AccessToken" /> using the specified value.</summary>
        /// <param name="token">The value to use.</param>
        /// <param name="expires">The date/time that the token will expire.</param>
        public AccessToken(String token, DateTime expires)
            : this()
        {
            this.Value = token;
            this.Expires = expires;
        }

        /// <summary>Gets the underlying <see cref="String" /> value.</summary>
        [DataMember(Name = "value")]
        public String Value { get; private set; }

        private Int64? _userID;
        /// <summary>Gets the FBGraph User ID the <see cref="AccessToken" /> has been granted for.</summary>
        [IgnoreDataMember]
        public Int64 UserID
        {
            get
            {
                if (!_userID.HasValue)
                {
                    _userID = Int64.Parse(this.Value.Split('|')[1].Split('-')[1]);
                }
                return _userID.Value;
            }
        }

        /// <summary>Gets the expiration date/time of the <see cref="AccessToken" />.</summary>
        [DataMember(Name = "expires")]
        public DateTime Expires { get; internal set; }

        /// <summary>Returns the underlying <see cref="String" /> value of the <see cref="AccessToken" />.</summary>
        public override String ToString()
        {
            return Value;
        }
    }
}
